package com.example.yjh.zyfypt_314yjh.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import android.widget.Toast;

import com.example.yjh.zyfypt_314yjh.R;
import com.example.yjh.zyfypt_314yjh.bean.LoginBean;
import com.example.yjh.zyfypt_314yjh.iface.LoginListener;
import com.example.yjh.zyfypt_314yjh.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et1,et2;
    private Button bt1,bt2;
    private Switch sw;
    private String username="",password="",sessionID="",realname="";
    private SharedPreferences sp;
    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                Intent intent2 = new Intent();
                intent2.setClass(LoginActivity.this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
                sessionID=loginBean.getSessionid().toString();
                realname=loginBean.getRealname().toString();
                System.out.println("----sessionID="+sessionID);
                System.out.println("----realname="+realname);
                saveSP();
                //   String SessionID=list.get(1).getSessionid();
                //    System.out.println(SessionID);
                //  intent2.putExtra("sessionid",SessionID);
              //  Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                startActivity(intent2);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Fail();
        }
    };


    private void Fail() {
        Toast.makeText(this, "用户名密码错误失败", Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void init() {
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        bt1=(Button)findViewById(R.id.BTlogin);
        bt2=(Button)findViewById(R.id.BTregister);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        sw=(Switch) findViewById(R.id.switch1);
    }


    @Override
    public void onClick(View v) {
        username=et1.getText().toString();
         password=et2.getText().toString();
      //  Intent intent=new Intent(LoginActivity.this,MainActivity.class);
        switch (v.getId()){
            case R.id.BTlogin:
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(et1.getText().toString(), et2.getText().toString(), loginListener);
                break;
            case R.id.BTregister:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionID",sessionID);
        editor.putString("realname",realname);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            et1.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }
}
